package com.example.home.attorney.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.attorney.bean.LawyerCardListBean;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class LegalExpertiseAdapter extends RecyclerView.Adapter<LegalExpertiseAdapter.ViewHolder> {
    private List<LawyerCardListBean.RowsBean> list;
    private BottomSheetDialog dialog;
    private Callback onItemClickListener;


    public interface Callback {
        void onItemClick(int position);
    }

    public LegalExpertiseAdapter(List<LawyerCardListBean.RowsBean> list, BottomSheetDialog dialog, Callback onItemClickListener) {
        this.list = list;
        this.dialog = dialog;
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layers, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {


        holder.severIvText.setText(list.get(position).getName());

        // 使用 Glide 加载图片
        Glide.with(dialog.getContext()).load(ip+list.get(position).getImgUrl()).into(holder.severTvImg);

        // 点击法律专长，处理相应的逻辑
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取点击的位置，并触发回调接口
                int position1 = list.get(position).getId();
                if (position1 != RecyclerView.NO_POSITION && onItemClickListener != null) {
                    onItemClickListener.onItemClick(position1);
                }
                dialog.getBehavior().setPeekHeight(100);
                // 隐藏对话框
                dialog.dismiss();
            }
        });
    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout liLayers;
        private ImageView severTvImg;
        private TextView severIvText;




        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            liLayers = itemView.findViewById(R.id.li_layers);
            severTvImg = itemView.findViewById(R.id.sever_tv_img);
            severIvText = itemView.findViewById(R.id.sever_iv_text);
        }
    }

}

